import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter/widgets.dart';
import 'package:get/get.dart';
import 'package:sainbainu/common/values/colors.dart';

/// 分享菜单种类
enum ShareMenuType {
  wechatSession,
  wechatTimeline,
  wechatFavorite,
}

/// 分享菜单实体
class _ShareMenuModel {
  final String name;
  final String icon;
  final ShareMenuType type;

  _ShareMenuModel({
    required this.name,
    required this.icon,
    required this.type,
  });
}

/// 全部菜单列表
List<_ShareMenuModel> _shareMenuAllList = [
  _ShareMenuModel(
    name: '微信好友',
    icon: 'assets/images/share-friend.png',
    type: ShareMenuType.wechatSession,
  ),
  _ShareMenuModel(
    name: '微信朋友圈',
    icon: 'assets/images/share-timeline.png',
    type: ShareMenuType.wechatTimeline,
  ),
  _ShareMenuModel(
    name: '微信收藏',
    icon: 'assets/images/share-collect.png',
    type: ShareMenuType.wechatFavorite,
  ),
];

typedef MenuTap = void Function(ShareMenuType type);

class ShareMenu extends StatefulWidget {
  /// 启用的菜单
  final List<ShareMenuType> enableMenus;

  final void Function()? onCancelTap;
  final MenuTap onMenuTap;

  const ShareMenu({
    super.key,
    this.enableMenus = const [ShareMenuType.wechatSession],
    this.onCancelTap,
    required this.onMenuTap,
  });

  @override
  State<ShareMenu> createState() => _ShareMenuState();
}

class _ShareMenuState extends State<ShareMenu> {
  late List<_ShareMenuModel> _shareMenuList;

  @override
  void initState() {
    super.initState();

    setState(() {
      _shareMenuList = _shareMenuAllList
          .where((menu) => widget.enableMenus.contains(menu.type))
          .toList();
    });
  }

  @override
  Widget build(BuildContext context) {
    return SafeArea(
      top: false,
      bottom: true,
      child: Column(
        mainAxisSize: MainAxisSize.min,
        children: [
          _buildTitle(),
          _buildMenu(),
          const SizedBox(height: 10),
          _buildGap(),
          _buildCancel(),
        ],
      ),
    );
  }

  Widget _buildTitle() {
    return Container(
      padding: const EdgeInsets.only(top: 20, bottom: 15),
      child: const Text(
        '分享到',
        style: TextStyle(fontSize: 18),
      ),
    );
  }

  /// 菜单
  Widget _buildMenu() {
    for (var menu in _shareMenuList) {
      _buildMenuItem(menu);
    }
    return SizedBox(
      height: 100,
      // child: ListView.builder(
      //   scrollDirection: Axis.horizontal,
      //   physics: const BouncingScrollPhysics(),
      //   padding: const EdgeInsets.symmetric(horizontal: 10),
      //   itemCount: _shareMenuList.length,
      //   itemBuilder: (context, index) {
      //     return _buildMenuItem(_shareMenuList[index]);
      //   },
      // ),
      child: Padding(
        padding: const EdgeInsets.symmetric(horizontal: 14),
        child: Row(
          mainAxisAlignment: MainAxisAlignment.spaceEvenly,
          children: _shareMenuList.map((m) => _buildMenuItem(m)).toList(),
        ),
      ),
    );
  }

  /// 菜单每一项
  Widget _buildMenuItem(_ShareMenuModel menu) {
    return InkWell(
      onTap: () => widget.onMenuTap(menu.type),
      borderRadius: BorderRadius.circular(8),
      child: SizedBox(
        width: 100,
        child: Column(
          children: [
            const SizedBox(height: 12),
            Image.asset(
              menu.icon,
              width: 56,
              height: 56,
            ),
            const SizedBox(height: 4),
            Text(
              menu.name,
              style: const TextStyle(color: AppColors.active, fontSize: 12),
            ),
          ],
        ),
      ),
    );
  }

  Widget _buildGap() {
    return Container(height: 10, color: AppColors.page);
  }

  Widget _buildCancel() {
    return InkWell(
      onTap: widget.onCancelTap,
      splashFactory: NoSplash.splashFactory,
      child: Container(
        height: 54,
        alignment: Alignment.center,
        child: const Text(
          '取消',
          style: TextStyle(color: AppColors.active, fontSize: 16),
        ),
      ),
    );
  }
}
